package cn.com.duiba.millionaire.center.biz.dao.impl;


import cn.com.duiba.millionaire.center.biz.bo.ConsumeAccountInfoParam;
import cn.com.duiba.millionaire.center.biz.bo.ConsumerRenascenceInfoParam;
import cn.com.duiba.millionaire.center.biz.bo.UserReLiveCardParam;
import cn.com.duiba.millionaire.center.biz.constants.DatabaseSchema;
import cn.com.duiba.millionaire.center.biz.dao.BaseDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireConsumerDao;
import cn.com.duiba.millionaire.center.biz.dao.TableHelper;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireConsumerEntity;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzy on 2018/1/17.
 */
@Service
public class MillionaireConsumerDaoImpl extends BaseDao implements MillionaireConsumerDao {
    @Override
    public MillionaireConsumerEntity insert(MillionaireConsumerEntity entity) {
        Map<String, Object> params = TableHelper.getTbSuffixParamsMap(entity.getConsumerId());
        params.put("entity", entity);
        insert("insert", params);
        return entity;
    }

    @Override
    public int batchInsert(List<MillionaireConsumerEntity> entitys, String sufix) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("tb_suffix", sufix);
        params.put("lists", entitys);
        return insert("batchInsert", params);
    }

    @Override
    public MillionaireConsumerEntity findByConsumerId(Long consumerId) {
        Map<String, Object> params = TableHelper.getTbSuffixParamsMap(consumerId);
        params.put("consumerId", consumerId);
        return selectOne("findByConsumerId", params);
    }

    @Override
    public int updateShareCode(Long consumerId, String shareCode) {
        Map<String, Object> params = TableHelper.getTbSuffixParamsMap(consumerId);
        params.put("shareCode", shareCode);
        params.put("consumerId", consumerId);
        return update("updateShareCode", params);
    }

    @Override
    public int updateRenascenceNum(Long consumerId, Integer renascenceNum) {
        Map<String, Object> params = TableHelper.getTbSuffixParamsMap(consumerId);
        params.put("consumerId", consumerId);
        params.put("renascenceNum", renascenceNum);
        return update("updateRenascenceNum", params);
    }


    @Override
    public List<MillionaireConsumerEntity> batchFindConsumer(List<Long> consumerIds, String sufix) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("tb_suffix", sufix);
        params.put("consumerIds", consumerIds);
        return selectList("findByConsumerIds", params);
    }

    @Override
    public int batchResumeConsumerRenascence(List<ConsumerRenascenceInfoParam> cidAndNums, String sufix) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("tb_suffix", sufix);
        params.put("list", cidAndNums);
        return update("batchResumeRenascenceNum", params);
    }

    @Override
    public int batchAddConsumerAccount(List<ConsumeAccountInfoParam> cidAndNums, String sufix) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("tb_suffix", sufix);
        params.put("list", cidAndNums);
        return update("batchAddAccountNum", params);
    }

    @Override
    public MillionaireConsumerEntity select4Update(long consumerId) {
        Map<String, Object> param = new HashMap<>();
        param.put("consumerId", consumerId);
        param.put("tb_suffix", TableHelper.getTableSuffix(consumerId));
        return selectOne("select4Update", param);
    }

    @Override
    public boolean withdrawCash(long consumerId, int changeAmount) {
        Map<String, Object> param = new HashMap<>();
        param.put("consumerId", consumerId);
        param.put("changeAmount", changeAmount);
        param.put("tb_suffix", TableHelper.getTableSuffix(consumerId));
        return update("withdrawCash", param) == 1;
    }

    @Override
    public boolean withdrawCashPayback(long consumerId, int changeAmount) {
        Map<String, Object> param = new HashMap<>();
        param.put("consumerId", consumerId);
        param.put("changeAmount", changeAmount);
        param.put("tb_suffix", TableHelper.getTableSuffix(consumerId));
        return update("withdrawCashPayback", param) == 1;
    }

    @Override
    public int batchUpdateRenascenceNum(List<UserReLiveCardParam> param, String sufix) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("tb_suffix", sufix);
        params.put("list", param);
        return update("batchAddResumeRenascenceNum", params);
    }

    @Override
    protected DatabaseSchema chooseSchema() {
        return DatabaseSchema.MILLIONAIRE_CONSUMER;
    }
}
